{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.preprocessing import PolynomialFeatures\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.pipeline import Pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "x=np.arange(1,11)\n",
    "y=x**2 * 0.5 + x * 0.7 + 1 + np.random.normal(len(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x1a20533e48>"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAD9VJREFUeJzt3W9sXXd9x/H3d04qTAG5pW6VOHQBKTKgodbMQmWV0NbAzAaiFqIT04YiVClPECsbMmt4Mk2aRJEn/jxCi9oxS+uALpikQghThaJpT7qldbcAwcrooMQOjWF4MLgaafbdA5+Q/7nn2r4+9/78fknRPed3z9H56ij++Ph3fuf8IjORJPW/X2u6AEnSxjDQJakQBrokFcJAl6RCGOiSVAgDXZIKYaBLUiEMdEkqhIEuSYXYtpkHu+WWW3L37t2beUhJ6ntPP/30jzJzuN12mxrou3fv5tixY5t5SEnqexHx/Trbte1yiYjRiHj2on8/jYgPRcTNEfFERJysPm9af9mSpLVqG+iZuZCZd2bmncBvAr8AvgQ8CBzNzD3A0WpdktSQTm+K7gW+m5nfB+4FZqr2GWByIwuTJ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CTCemb8BDADvbbaqTfd3wNsva3sQOJqZe4Cj1fqGM9BryszTmflMtfwzVn9gt+x7fSNiF/AO4OGma2laRLwCeAvwCEBm/jIzV5qtqlHbgMGI2Aa8FFhquJ5NlZn/BPzXZc33AjPV8gww2Y1jG+hrEBG7gTHgqWYradSngI8A/9d0IT3gNcAy8NmqC+rhiLix6aKakJmLwF8DzwOngf/OzK81W1VPuC0zT8PqxSFwazcOYqB3KCJeBnwR+FBm/rTpepoQEe8EzmTm003X0iO2AW8EPpOZY8DP6dKf1L2u6hu+F3g1sBO4MSL+uNmqtg4DvQMRsZ3VMH80M2ebrqdBdwPviojvAZ8H7omIv2+2pEadAk5l5vm/2A6xGvBb0VuB/8zM5cw8C8wCv9VwTb3ghYjYAVB9nunGQQz0miIiWO0jPZGZn2i6niZl5oHM3JWZu1m94fX1zNyyV2GZ+UPgBxFxfvbyvcC3GyypSc8Dd0XES6ufmb1s0RvEl3kc2Fct7wOOdOMgPT+naA+5G3gfcDwinq3aPpqZX2mwJvWODwKPRsQNwHPA+xuupxGZ+VREHAKeYXVk2Dxb7InRiPgc8NvALRFxCvgL4CHgsYi4n9Vfevd15dg+KSpJZbDLRZIKYaBLUiEMdEkqhIEuSYUw0CWpEAa6JBXCQJekQhjoklSI/wfDWNV5jlnSgAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1a206f4588>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=x.reshape(-1,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1],\n",
       "       [ 2],\n",
       "       [ 3],\n",
       "       [ 4],\n",
       "       [ 5],\n",
       "       [ 6],\n",
       "       [ 7],\n",
       "       [ 8],\n",
       "       [ 9],\n",
       "       [10]])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [],
   "source": [
    "po=PolynomialFeatures(degree=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PolynomialFeatures(degree=3, include_bias=True, interaction_only=False)"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "po.fit(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_t=po.transform(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   1.,    1.,    2.,    1.,    2.,    4.,    1.,    2.,    4.,\n",
       "           8.],\n",
       "       [   1.,    3.,    4.,    9.,   12.,   16.,   27.,   36.,   48.,\n",
       "          64.],\n",
       "       [   1.,    5.,    6.,   25.,   30.,   36.,  125.,  150.,  180.,\n",
       "         216.],\n",
       "       [   1.,    7.,    8.,   49.,   56.,   64.,  343.,  392.,  448.,\n",
       "         512.],\n",
       "       [   1.,    9.,   10.,   81.,   90.,  100.,  729.,  810.,  900.,\n",
       "        1000.]])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([12.99515631, 15.19515631, 18.39515631, 22.59515631, 27.79515631,\n",
       "       33.99515631, 41.19515631, 49.39515631, 58.59515631, 68.79515631])"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr=LinearRegression()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr.fit(X_t,y[:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_p=lr.predict(X_t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([12.99515631, 15.19515631, 18.39515631, 22.59515631, 27.79515631])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [],
   "source": [
    "pl=Pipeline([\n",
    "    ('poly',PolynomialFeatures(degree=2)),\n",
    "    ('std_scaler',StandardScaler()),\n",
    "    ('lr',LinearRegression())\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Pipeline(memory=None,\n",
       "     steps=[('poly', PolynomialFeatures(degree=2, include_bias=True, interaction_only=False)), ('std_scaler', StandardScaler(copy=True, with_mean=True, with_std=True)), ('lr', LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False))])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pl.fit(X,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([12.99515631, 15.19515631, 18.39515631, 22.59515631, 27.79515631,\n",
       "       33.99515631, 41.19515631, 49.39515631, 58.59515631, 68.79515631])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pl.predict(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([12.99515631, 15.19515631, 18.39515631, 22.59515631, 27.79515631,\n",
       "       33.99515631, 41.19515631, 49.39515631, 58.59515631, 68.79515631])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
